﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NumericalData.Interpolation.Data;

namespace NumericalData.Interpolation.Data
{
	public class ExpressionContainer
	{
		#region Fields

		private double _intervalStart;
		private double _intervalStop;
		private EquidistantPointsGrid _ptsGrid = null;
		private string _expression;
		private string _variableName;
		private string _status;
		private bool _expressionIsValid = false;
		private int _methodOrder;
		#endregion

		#region Constructors
		public ExpressionContainer(string exp, string name, double start, double stop, EquidistantPointsGrid grid, int methodOrder)
		{
			_expression = exp;
			_intervalStart = start;
			_intervalStop = stop;
			_ptsGrid = grid;
			_variableName = name;
			if (_ptsGrid == null)
			{
				_status = "Grid is null";
				_expressionIsValid = false;
			}
			else
			{
				_status = "Expression is Valid";
				_expressionIsValid = true;
			}
			_methodOrder = methodOrder;
		}

		public ExpressionContainer(string exp, string name, double start, double stop, int numberOfPoints, int methodOrder)
		{
			_expression = exp;
			_intervalStart = start;
			_intervalStop = stop;
			_variableName = name;
			try
			{
				_ptsGrid = EquidistantPointsGrid.ExecuteFromFunction(exp, name, start, stop, numberOfPoints);
				_status = "Expression is Valid";
				_expressionIsValid = true;
			}
			catch (Exception ex)
			{
				_ptsGrid = null;
				_status = ex.Message;
				_expressionIsValid = false;
			}
			_methodOrder = methodOrder;
		}
		#endregion

		#region Properties
		public string Expression { get { return _expression; } }

		public double IntervalStart
		{
			get { return _intervalStart; }
		}

		public double IntervalStop
		{
			get { return _intervalStop; }
		}

		public EquidistantPointsGrid Grid
		{
			get { return _ptsGrid; }
		}

		public string VariableName
		{
			get { return _variableName; }
		}

		public string Status
		{
			get { return _status; }
		}

		public bool IsValid
		{
			get { return _expressionIsValid; }
		}

		public int MethodOrder
		{
			get { return _methodOrder; }
		}

		public bool LoadedFromFile { get; set; }

		#endregion

		#region Methods
		public override int GetHashCode()
		{
			int acc = 0;
			acc += _ptsGrid.Count + (int)Math.Abs(_intervalStart * 2) + (int)Math.Abs(_intervalStop * 0.1) + _methodOrder + _expression.GetHashCode();
			return acc;
		}
		#endregion

	}
}
